<?php
namespace app\wap\controller;

use think\Controller;
use app\index\model\Project;
use app\index\model\Archive;
use app\index\model\Carousel;
use app\index\model\Notice;
use think\Config;
use think\Request;

class Index extends Common
{
    const PERPAGE = 10;
    public function _initialize(){
        parent::_initialize();

    }

    /**
     * 首页
     *  @param Request $request
     * @return json $data
     * @author kangyi(997032813@qq.com)
     */
    public function index(Archive $aModel,Carousel $cmodel)
    {
        $where['sort'] = ['<',13];
        $dataList = $cmodel->selectInfo($where,'sort asc','*');
        $cwhere['status'] = ['eq',1];
        $cwhere['typeid'] = ['eq',2];
        $cateList['list'][0] = $aModel->findInfo($cwhere);
        $cwhere['typeid'] = ['eq',3];
        $cateList['list'][1] = $aModel->findInfo($cwhere);
        $cwhere['typeid'] = ['eq',1];
        $cateList['list'][2] = $aModel->findInfo($cwhere);
        $data = [
            'banner_list' => banners(3,5),
            'fink_list'   => flinks(1),
            'cate_list'   => $cateList,
            'data_list'   => $dataList,
        ];
        outPutData($data);
    }

    /**
     * 关于我们
     * @author kangyi(997032813@qq.com)
     */

    public function about()
    {
        $arcModel = new Archive();;
        $arcList = $arcModel->getAbout();
        outPutData($arcList);
    }


    /**
     * 消息通知
     * @author kangyi(997032813@qq.com)
     */
    public function notice(Request $request){
        $param = $request->param();
        if (empty($param['curpage'])) {
            $curpage = 1;
        } else {
            $curpage = $param['curpage'];
        }
        $NoModel = new Notice();
        $user_info = $request->user_info;
        if(request()->isPost('id') && input('id')){
            $id = input('post.id');
            $noticeInfo = $NoModel->where(['id'=>$id])->find();
            if($noticeInfo['isread'] !=1){
                $NoModel->where(['id'=>$id])->update(['isread'=>1]);
            }
            outPutData($noticeInfo['content'],'',Config::get('code.success'));
        }
        $where = ['uid'=>$user_info['id']];
        $dataList = $NoModel->where($where)->order("create_time DESC")->select()->toArray();

        $total_count = count($dataList);
        $total_page = ceil($total_count/self::PERPAGE);
        $data_list = pageArray($dataList,self::PERPAGE,$curpage);
        $data =array('list'=>$data_list,'total_page'=>$total_page,'total_count'=>$total_count) ;
        outPutData($data);
    }

    /**
     * 消息通知
     * @author kangyi(997032813@qq.com)
     */
    public function affiche(Request $request){
        $param = $request->param();
        if (empty($param['curpage'])) {
            $curpage = 1;
        } else {
            $curpage = $param['curpage'];
        }
        $artModel = new Archive();
        $where['typeid'] = 2;
        $where['status'] = 1;
        $where['mod'] = 'addonarticle';

        $dataList = $artModel->where($where)->order("create_time DESC")->select()->toArray();
        $total_count = count($dataList);
        $total_page = ceil($total_count/self::PERPAGE);
        $data_list = pageArray($dataList,self::PERPAGE,$curpage);
        $data =array('list'=>$data_list,'total_page'=>$total_page,'total_count'=>$total_count) ;
        outPutData($data);
    }

    /**
     * 好友列表
     * @author cpy(1442435897@qq.com)
     */
    public function inviteList(Request $request, \app\wap\model\Member $member){
        $user_info = $request->user_info;
        $param = $request->param();
        $pageNumber = $param['curpage'] ? $param['curpage'] : 1;
        $pageSize = $param['pageSize'] ? $param['pageSize'] : 10;
        $limit = ($pageNumber - 1) * $pageSize . ',' . $pageSize;
        $where['parentId'] = $user_info['id'];
        if($param['keywords']){
            $where['username'] = ['Like','%'.$param['keywords'].'%'];
        }
        $res = $member->selectInfo($where,$limit,'username,is_check,mobile,head_thumb,create_time');
        foreach ($res as $k => $v){
            $res[$k]['mobile'] = maskMobile($v['mobile']);
        }
        $row = $member->selectInfo($where,'id');
        $result = Config::get('code.success');
        $result['data'] = $res;
        $result['total_count'] = count($row);
        $result['total_page'] = ceil($result['total_count']/$pageSize);
        return json($result);
    }
}
